Amber's Digital Garden

Unreal C++ For Loops

Literally just remembering some basic for-loop syntax.


Here are a few different ways to declare for loops in C++

For Loop typical syntax: for (variable; condition; update variable operation)

Classic range-based for loop:

for (int i = 0; i < 5; i++)

"i" can start with a different value:

for (int i = 1; i < 5; i++)

"i" can be a different type (including custom types). If custom types are used, the required operators must be overridden properly.:

for (double i = 0.0; i < 5.0; i++)

Any of the comparison operators can be used in the condition ("<", "<=", ">", ">=" etc):

for (int i = 0; i <= 5; i++)

Since this is a condition, Boolean operators (&&, ||, ! etc) can be used (although not as useful):

for (int i = 1; (i < 5 && i < 2); i++)

The last term can contain a large variety of expressions to update "i". "i" can be incremented (++) or decremented (--):

Sources:


by amber